<template>
  <div class="app-container">
    <el-dialog :title="titleList[flag]" :visible.sync="dialogVisible" width="30%" append-to-body>
      <el-form ref="form" :model="form">
        <el-form-item label="父级类型" prop="superCategoryId">
          <treeselect v-model="form.superCategoryId" :options="gooTypeList" :show-count="true"
                      placeholder="请选择" :normalizer="normalizer" :disabled="flag!=0"/>
        </el-form-item>
        <el-form-item label="商品类型" prop="productCategoryName" required>
          <el-input v-model="form.productCategoryName" placeholder="请输入" maxlength="50"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="dialogOk">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addGoodType, updateGoodType} from "../../../api/imsMng";
import Treeselect from "@riophae/vue-treeselect";
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  props: ['gooTypeList'],
  components: {Treeselect},
  data() {
    return {
      flag: 0,
      loading: false,
      dialogVisible: false,
      form: {},
      titleList: ['新增', '新增', '修改'],
      normalizer(node) {
        return {
          id: node.id,
          label: node.productCategoryName,
          children: node.productCategoryVoList,
        }
      },
    }
  },
  methods: {
    show(flag, form) {
      this.flag = flag
      this.dialogVisible = true
      this.resetForm('form')
      this.form = form
      if (this.form.superCategoryId == 0) {
        this.form.superCategoryId = null
      }
    },
    dialogOk() {
      this.$refs.form.validate(e => {
        if (e) {
          if (this.flag == 2) {
            updateGoodType(this.form).then(res => {
              this.$emit('ok')
              this.dialogVisible = false
              this.$message.success('修改成功')
            })
          } else {
            addGoodType(this.form).then(res => {
              this.$emit('ok')
              this.dialogVisible = false
              this.$message.success('新增成功')
            })
          }
        }
      })
    },
  }
}
</script>
